//
//  ReadingViewController.m
//  LineByLine
//
//  Created by 병철 김 on 11. 9. 29..
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "ReadingViewController.h"
#import "CommonDefine.h"
#import "CoreDataManager.h"
#import "StudyManager.h"
#import "CommonUtil.h"


@implementation ReadingViewController

#pragma mark - init/dealloc
- (void)configView {
    /** draw table view */
    NSArray *paragraphs = [[CoreDataManager sharedCoreDataManager] selectFrom:@"Sentence" where:@"book=%@ and paragraph=1", book];
    readingView = [[Step1ReadingView alloc] initWithFrame:CGRectMake(0, 0, kContentWidth, kContentHeight)];
    [readingView setDataSource:self];
    [readingView setViewDelegate:self];
    [readingView setParagraphs:paragraphs];
    [readingView setPageCount:[[book valueForKey:@"lines"] intValue]];
    [self.view addSubview:readingView];
    [readingView release];
}

- (void)dealloc {
    [super dealloc];
}

- (void)hideDictionaryLabel {
    if (dictionaryLabel && ![dictionaryLabel isHidden]) {
        [dictionaryLabel setHidden:YES];
    }
}

#pragma mark - StepViewController Dynamic Method
- (StepView*) stepView {
    return readingView;
}

#pragma mark - tableview datasource
- (NSInteger)numberOfSections {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [[book valueForKey:@"lines"] intValue];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    [self hideDictionaryLabel];
    static NSString *cellId = @"PairTableViewCell";
    PairTableViewCell *cell = (PairTableViewCell*)[tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell) {
        cell = [[[PairTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId] autorelease];
        CGRect frame = cell.frame;
        frame.size.height = kReadViewHeight;
        [cell setFrame:frame];
        [cell setViewDelegate:self];
    }
    
    id sentence = [[CoreDataManager sharedCoreDataManager] selectOneFrom:@"Sentence" where:@"book=%@ and sid=%@", book, [NSNumber numberWithInt:(indexPath.row + 1)], nil];
    [cell setSentence:sentence];
    [cell setRowId:indexPath.row];
    [cell setCol:curCol];
    [cell configView];
    return cell;
}

#pragma mark - PairTableViewCellDelegate
- (void)colDidChanged:(int)col {
    curCol = col;
}

- (void)didChangePage:(NSInteger)page {
    [super didChangePage:page];
    [readingView drawProgressViewMarker];
}

- (void) onClickText:(ShadowedTextView*) textView {
    if (textView == nil) {
        [[DictionaryManager sharedDictionaryManager] cancelSearching];
        [self hideDictionaryLabel];
        return;
    }
    
    NSString *word = [[textView text] stringByTrimmingCharactersInSet:[NSCharacterSet punctuationCharacterSet]];
    
    [[DictionaryManager sharedDictionaryManager] setDelegate:self];
    [[DictionaryManager sharedDictionaryManager] simpleMeaning:word];
    
    if (!dictionaryLabel) {
        dictionaryLabel = [[DictionaryLabel alloc] init];
        [self.view addSubview:dictionaryLabel];
        [dictionaryLabel release];
    }
    
    CGRect superViewFrame = textView.superview.frame;
    CGRect frame = superViewFrame;
    frame.size.height = 30;
    frame.origin.y = superViewFrame.origin.y + textView.frame.origin.y + 15;
    
    [dictionaryLabel setFrame:frame];
    [dictionaryLabel setHidden:NO];
    [dictionaryLabel setSearchWord:word];
}

#pragma mark - DictionaryDelegate
- (void)didFinishDictionarySeachingWord:(NSString *)word meaning:(NSString *)meaning {
    if ([dictionaryLabel isEqualSearchStringIgnoreCase:word]) {
        if ([meaning length] <= 0 && [word hasSuffix:@"s"]) {
            // 복수형이어서 값이 없다면 단수형으로 바꿔서 다시 
            NSString *singularWord = [CommonUtil singularWord:word];
            [dictionaryLabel setSearchWord:singularWord];
            [[DictionaryManager sharedDictionaryManager] simpleMeaning:singularWord];
        } else {
            [dictionaryLabel setMeaning:meaning];
        }
    } else {
        [dictionaryLabel setMeaning:@"..."];
    }
}
@end
